{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="SHOP_CSS/evaluate.css" />
<style>
	.layui-table{margin: 15px 0;}
	tbody .title-content{}
	.layui-inline{margin-bottom: 15px !important;}
	.tab-bottom{display: flex;justify-content: space-between;}
	.evaluate-table tr td:nth-child(5),.evaluate-table tr td:nth-child(6){padding-right: 0px;}
	.evaluate-table tr td:nth-child(6){padding-left: 0px;}
	.layui-layout-admin.admin-style-2 .layui-form-item .layui-input-inline{background-color: #fff;}
	.layui-layout-admin.admin-style-2 .table-bottom{padding: 15px 0 10px;}
	.title-content .sku-name {overflow:hidden;text-overflow:ellipsis;display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:2;}
</style>
{/block}
{block name="main"}

<!-- 搜索框 -->
<div class="screen layui-collapse" >
	<div class="layui-colla-item">
		<form class="layui-colla-content layui-form layui-show" lay-filter="selection_panel">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">搜索类型</label>
					<div class="layui-input-inline">
						<select name="search_type">
							<option value="sku_name">商品名称</option>
							<option value="member_name">评价人名称</option>
						</select>
					</div>
					<div class="layui-input-inline">
						<input type="text" name="search_text" placeholder="请输入搜索内容" class="layui-input len-mid" autocomplete="off">
					</div>
				</div>


				<div class="layui-inline">
					<label class="layui-form-label">评价类型</label>
					<div class="layui-input-inline">
						<select name="explain_type">
							<option value="">请选择评分类型</option>
							<option value="1">好评</option>
							<option value="2">中评</option>
							<option value="3">差评</option>
						</select>
					</div>
				</div>

				
				<div class="layui-inline">
					<label class="layui-form-label">评论时间</label>
					<div class="layui-input-inline">
						<input type="text" name="start_time" id="start_time" placeholder="开始时间" class="layui-input" autocomplete="off" readonly>
						<i class=" iconrili iconfont calendar"></i>
					</div>
					<div class="layui-form-mid">-</div>
					<div class="layui-input-inline end-time">
						<input type="text" name="end_time" id="end_time" placeholder="结束时间" class="layui-input" autocomplete="off" readonly>
						<i class=" iconrili iconfont calendar"></i>
					</div>
				</div>
			</div>
			<input type="hidden" name="goods_id" value="{$goods_id}">
			<div class="form-row">
				<button class="layui-btn" lay-submit lay-filter="search">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</form>
	</div>
</div>


<div class="layui-tab table-tab" lay-filter="is_audit">
	<ul class="layui-tab-title">
		<li class="layui-this" lay-id="">全部</li>
		<li lay-id="1">未审核</li>
		<li lay-id="2">审核通过</li>
		<li lay-id="3">审核拒绝</li>
	</ul>

	<div class="layui-tab-content">
		<div class="table-bottom layui-table-bottom-tool-temp layui-form" style="float: left;border: none; padding-left: 15px;">
			<input type="checkbox" name="check_all" lay-skin="primary" lay-filter="check_all">
			<button class="layui-btn layui-btn-primary js-audit-pass" data-is-audit="1">批量通过</button>
			<button class="layui-btn layui-btn-primary js-audit-refuse" data-is-audit="2">批量拒绝</button>
			<button class="layui-btn layui-btn-primary js-audit-delete" data-is-audit="3">批量删除</button>
		</div>

		<table class="layui-table evaluate-table layui-form" lay-skin="line" lay-size="lg">
			<div class="layui-tab-content">
				<colgroup>
					<col width="3%">
					<col width="20%">
					<col width="10%">
					<col width="10%">
					<col width="27%">
					<col width="10%">
					<col width="10%">
					<col width="10%">
				</colgroup>
				<thead>
				<tr>
					<th><input type="checkbox" name="check_all" lay-skin="primary" lay-filter="check_all" /></th>
					<th>商品信息</th>
					<th>客户昵称</th>
					<th>评论等级</th>
					<th>评论详情</th>
					<th>评论时间</th>
					<th style="text-align: center;">审核状态</th>
					<th class = 'operate'>操作</th>
				</tr>
				</thead>
				<tbody></tbody>
			</div>
		</table>

		<div class="tab-bottom">
			<div class="table-bottom layui-table-bottom-tool-temp layui-form" style="float: left;border: none; padding-left: 15px;">
				<input type="checkbox" name="check_all" lay-skin="primary" lay-filter="check_all">
				<button class="layui-btn layui-btn-primary js-audit-pass" data-is-audit="1">批量通过</button>
				<button class="layui-btn layui-btn-primary js-audit-refuse" data-is-audit="2">批量拒绝</button>
				<button class="layui-btn layui-btn-primary js-audit-delete" data-is-audit="3">批量删除</button>
			</div>
			<div id="laypage"></div>
		</div>
	</div>
</div>


{/block}
{block name="script"}
<script>
	var SHOPIMG = "SHOP_IMG";
</script>
<script src="SHOP_JS/evaluate.js"></script>
<script>
	var limit = 5;
	var evaluate = new Evaluate(limit, [2, 5, 10]);
	var goods_id = "{$goods_id}";
	evaluate.getList({
		"_this": evaluate,
		"goods_id" : goods_id,
	});
	evaluate.pageInit({
		"_this": evaluate,
		"goods_id" : goods_id,
	});

	layui.use(['form', 'laydate','element'], function() {
		var form = layui.form,
			laydate = layui.laydate;
			element = layui.element;
		currentDate = new Date(),
			minDate = "";
		form.render();

		//监听Tab切换
		element.on('tab(is_audit)', function(data) {
			var is_audit = $(this).attr("lay-id");

			var num = $(".layui-laypage-limits  option:selected").val()
			var evaluate = new Evaluate(num, [2, 5, 10]);
			data = form.val('selection_panel');
			evaluate.getList({
				"_this": evaluate,
				"search_type": data.search_type,
				"search_text": data.search_text,
				"explain_type": data.explain_type,
				"start_time": data.start_time,
				"end_time": data.end_time,
				"goods_id" : data.goods_id,
				"is_audit" : is_audit,
			});
			evaluate.pageInit({
				"_this": evaluate,
				"search_type": data.search_type,
				"search_text": data.search_text,
				"explain_type": data.explain_type,
				"start_time": data.start_time,
				"end_time": data.end_time,
				"goods_id" : data.goods_id,
				"is_audit" : is_audit,
			});
			return false;
		});

		currentDate.setDate(currentDate.getDate() - 7);

		form.on('checkbox(check_all)', function(data){
			$("input[name='evaluate']").prop("checked",data.elem.checked);
			$("input[name='check_all']").prop("checked",data.elem.checked);
			form.render();
		});

		form.on('checkbox(evaluate)', function(data){
			if($("input[name='evaluate']:checked").length == limit){
				$("input[name='check_all']").prop("checked",true);
			} else {
				$("input[name='check_all']").prop("checked",false);
			}
			form.render();
		});

		$(".js-audit-pass,.js-audit-refuse,.js-audit-delete").click(function () {
			var evaluate_ids = [];
			var is_audit = $(this).attr("data-is-audit");
			$("input[name='evaluate']:checked").each(function () {
				evaluate_ids.push($(this).val());
			});

			if(evaluate_ids.length<1) {
				layer.msg("请选择要操作的数据");
				return;
			}
			if(is_audit==3){
				deleteEvaluate(evaluate_ids.toString());
			}else{
				modifyAuditEvaluate(evaluate_ids.toString(),is_audit);
			}

		});

		//开始时间
		laydate.render({
			elem: '#start_time',
			type: 'datetime'
		});

		//结束时间
		laydate.render({
			elem: '#end_time',
			type: 'datetime'
		});

		/**
		 * 重新渲染结束时间
		 */
		function reRender() {
			$("#end_time").remove();
			$(".end-time").html('<input type="text" class="layui-input" placeholder="结束时间" name="end_time" id="end_time" >');
			laydate.render({
				elem: '#end_time',
				type: 'datetime',
				min: minDate
			});
		}

		/**
		 * 搜索
		 */
		form.on('submit(search)', function(data) {
			var num = $(".layui-laypage-limits  option:selected").val()
			var evaluate = new Evaluate(num, [2, 5, 10]);
			var is_audit = $('.layui-tab-title .layui-this').attr('lay-id');
			evaluate.getList({
				"_this": evaluate,
				"search_type": data.field.search_type,
				"search_text": data.field.search_text,
				"explain_type": data.field.explain_type,
				"start_time": data.field.start_time,
				"end_time": data.field.end_time,
				"goods_id" : data.field.goods_id,
				"is_audit" : is_audit,
			});
			evaluate.pageInit({
				"_this": evaluate,
				"search_type": data.field.search_type,
				"search_text": data.field.search_text,
				"explain_type": data.field.explain_type,
				"start_time": data.field.start_time,
				"end_time": data.field.end_time,
				"goods_id" : data.field.goods_id,
				"is_audit" : is_audit,
			});
			return false;
		});
	});

	function replay(e) {
		var input = $(e).parents("tr").find(".evaluate_id");
		var evaluate_id = input.val();
		var isFirstExplain = input.attr("data-is-first-explain");

		layer.prompt({
			formType: 2,
			value: '',
			title: '请输入回复内容',
			area: ['350px', '150px'] ,//自定义文本域宽高
			yes: function(index, layero) {
				var explain = layero.find(".layui-layer-input").val();
				
				if (explain) {
					$.ajax({
						url: ns.url("shop/goods/evaluateapply"),
						data: {
							evaluate_id: evaluate_id,
							explain: explain,
							is_first_explain: isFirstExplain
						},
						dataType: 'JSON', //服务器返回json格式数据
						type: 'POST', //HTTP请求类型
						success: function(res) {
							if (res.code == 0) {
								window.location.reload();
							} else {
								layer.closeAll();
								evaluate.getList({
									"_this": evaluate,
									'is_audit' : $(".layui-tab-title").find('.layui-this').attr('lay-id')
								});

								layer.msg(res.message);

							}
						}
					});
					layer.close(index);
				} else {
					layer.msg('请输入回复内容!', {icon: 5, anim: 6});
				}
			}
		});
	}
	
	var repeat_flag = false;
	function deleteContent(e, isFirst) {
		var input = $(e).parents("tr").find(".evaluate_id");
		var evaluate_id = input.val();
		
		if(repeat_flag) return false;
		repeat_flag = true;

		layer.confirm('确定要删除回复内容吗?', function() {
			$.ajax({
				url: ns.url("shop/goods/deleteContent"),
				data: {
					evaluate_id: evaluate_id,
					is_first: isFirst
				},
				dataType: 'JSON', //服务器返回json格式数据
				type: 'POST', //HTTP请求类型
				success: function(res) {
					repeat_flag = false;
					if (res.code == 0) {
						evaluate.getList({
							"_this": evaluate,
							'is_audit' : $(".layui-tab-title").find('.layui-this').attr('lay-id')
						});

						layer.closeAll();
					} else {
						layer.closeAll();
						layer.msg(res.message);
					}
				}
			});
		}, function () {
			layer.close();
			repeat_flag = false;
		});
	}

	// 修改首评审核状态
	function audit(e,is_audit) {
		var input = $(e).parents("tr").find(".evaluate_id");
		var evaluate_id = input.val();
		modifyAuditEvaluate(evaluate_id,is_audit);
	}
	// 修改删除评论操作之前
	function toDelete(e) {
		var input = $(e).parents("tr").find(".evaluate_id");
		var evaluate_id = input.val();
		deleteEvaluate(evaluate_id);
	}

	function modifyAuditEvaluate(evaluate_ids,is_audit) {
		var hint = '确定要审核通过吗?';
		if(is_audit == 2){
			hint = '确定要拒绝审核吗?';
		}
		layer.confirm(hint, function() {
			$.ajax({
				url: ns.url("shop/goods/modifyAuditEvaluate"),
				data: {
					evaluate_ids: evaluate_ids,
					is_audit: is_audit
				},
				dataType: 'JSON', //服务器返回json格式数据
				type: 'POST', //HTTP请求类型
				success: function (res) {
					layer.msg(res.message);
					if (res.code == 0) {
						evaluate.getList({
							"_this": evaluate,
							'is_audit' : $(".layui-tab-title").find('.layui-this').attr('lay-id')
						});
					}
				}
			});
		});
	}

	function deleteEvaluate(evaluate_ids) {
		if(repeat_flag) return false;
		repeat_flag = true;
		layer.confirm('确定要删除评论吗?', function() {
		$.ajax({
			url: ns.url("shop/goods/deleteEvaluate"),
			data: {
				evaluate_ids: evaluate_ids,
			},
			dataType: 'JSON', //服务器返回json格式数据
			type: 'POST', //HTTP请求类型
			success: function (res) {
				repeat_flag = false;
				layer.msg(res.message);
				if (res.code == 0) {
					evaluate.getList({
						"_this": evaluate,
						'is_audit' : $(".layui-tab-title").find('.layui-this').attr('lay-id')
					});
					location.reload();
				}
			}
		});
		}, function () {
			layer.close();
			repeat_flag = false;
		});
	}

	function again_audit(e,is_audit) {
		var input = $(e).parents("tr").find(".evaluate_id");
		var evaluate_id = input.val();
		modifyAgainAuditEvaluate(evaluate_id,is_audit);
	}

	function modifyAgainAuditEvaluate(evaluate_ids,is_audit) {
		$.ajax({
			url: ns.url("shop/goods/modifyAgainAuditEvaluate"),
			data: {
				evaluate_ids: evaluate_ids,
				again_is_audit: is_audit
			},
			dataType: 'JSON', //服务器返回json格式数据
			type: 'POST', //HTTP请求类型
			success: function (res) {
				layer.msg(res.message);
				if (res.code == 0) {
					evaluate.getList({
						"_this": evaluate,
						'is_audit' : $(".layui-tab-title").find('.layui-this').attr('lay-id')
					});
				}
			}
		});
	}
</script>
{/block}